<?php

namespace App\Admin\Controllers;

use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;

class UsersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑用户信息')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('添加新用户')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->id('Id');
        // $grid->name('用户名');
        // $grid->email('邮箱');
        $grid->nickname('昵称');
        $grid->avatar('头像')->lightbox(['width' => 50, 'height' => 50]);
        $grid->phone('联系电话');
        $grid->last_login_ip('最近登录IP');
        $grid->last_login_time('最近登录时间');
        $grid->created_at('注册时间');

        $grid->disableExport();
        $grid->actions(function ($actions) {
            // $actions->disableView();
            $actions->disableDelete();
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->column(1/2, function ($filter) {
                $filter->like('nickname', '昵称')->placeholder('请输入客户昵称(支持模糊查询)。。。');
            });

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        // $show = new Show(User::findOrFail($id));

        // $show->id('Id');
        // $show->name('用户名');
        // $show->email('邮箱');
        // $show->password('密码');
        // $show->nickname('昵称');
        // $show->openid('Openid');
        // $show->avatar('头像');
        // $show->phone('Phone');
        // $show->last_login_ip('最近登录IP');
        // $show->last_login_time('最近登录时间');

        $tab = new Tab();
        //基本信息
        $user = User::findOrFail($id);
        $user_base_info = [
            ['用户ID', $id],
            ['用户名', $user->name],
            ['微信昵称', $user->nickname],
            ['openid', $user->openid],
            ['手机号码', $user->phone],
            ['最近登录IP', $user->last_login_ip],
            ['最近登录时间', $user->last_login_time],
            ['<a href="javascript:history.back(-1);" class="btn btn-sm btn-primary"><i class="fa fa-reply"></i>返回&nbsp;&nbsp;</a>','']
        ];
        $headers_info = ['信息', '值'];
        $table_user_info = new Table($headers_info, $user_base_info);
        $tab->add('基本信息', $table_user_info);
        //地址信息
        $headers_info = ['省', '市', '区', '详细地址','联系人','联系电话'];
        $table = [];
        foreach ($user->addresses as $address) {
            $table[] = [$address->province, $address->city,$address->district,$address->address,$address->contact_name,$address->contact_phone];
        }
        $user_addresses = new Table($headers_info, $table);
        $tab->add('地址信息', $user_addresses);
        return $tab->render();
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('name', '用户名');
        $form->email('email', '邮箱');
        $form->text('nickname', '昵称');
        $form->text('openid', '微信Openid');
        $form->text('avatar', '头像');
        $form->mobile('phone', '手机');
        // 直接添加一对多的关联模型
        $form->hasMany('addresses', '收货地址', function (Form\NestedForm $form) {
            $form->text('province', '省')->rules('required');
            $form->text('city', '市')->rules('required');
            $form->text('district', '区')->rules('required');
            $form->text('address', '详细地址')->rules('required');
            $form->text('zip', '邮编');
            $form->text('contact_name', '联系人')->rules('required');
            $form->text('contact_phone', '联系电话')->rules('required');
        });
        return $form;
    }

    protected function showFormParameters($content)
    {
        $parameters = request()->except(['_pjax', '_token']);

        if (!empty($parameters)) {

            ob_start();

            dump($parameters);

            $contents = ob_get_contents();

            ob_end_clean();

            $content->row(new Box('Form parameters', $contents));
        }
    }
}
